#include "c_jsoninterpreter.h"
#include <string.h>


enum state __interprete_state(const char *state){
	enum state ret;
	if(strcmp(state, "Active") == 0)
		ret = active;
	else if(strcmp(state, "Preparation") == 0)
		ret = preparation;
	else if(strcmp(state, "Warmup") == 0)
		ret = warmup;
	else if(strcmp(state, "Fail") == 0)
		ret = fail;
	else if(strcmp(state, "Success") == 0)
		ret = success;
	else
		ret = undef;
	return ret;
}


eventPtr interprete_event_mapping(jPtr ptr){
	int i, j, size, buff_size;
	eventPtr result, buffer;
	result = 0;
	if(ptr->data_type == List && ptr->list_type == Object 
		&& strcmp("id", ptr->data[0].data[0].key) == 0 && strcmp("name", ptr->data[0].data[1].key) == 0){
			buff_size = 2;
			result = (eventPtr)malloc(sizeof(struct event)*buff_size);
			memset(result, 0, sizeof(struct event)*2);
			for(i = 0; ptr->data[i].data_type != end; i++){
				result[i].map_id = -1;
				result[i].world_id = -1;
				for(j = 0; ptr->data[i].data[j].data_type != end; j++){
					size = strlen(ptr->data[i].data[j].string)+1;
					if(j == 0){
						result[i].event_id = (char*)malloc(size);
						strcpy_s(result[i].event_id, size, ptr->data[i].data[j].string);
					}
					else{
						result[i].event_name = (char*)malloc(size);
						strcpy_s(result[i].event_name, size, ptr->data[i].data[j].string);
					}
				}
				buffer = result;
				result = (eventPtr)malloc(sizeof(struct event)*(buff_size+1));
				memset(result, 0, sizeof(struct event)*(buff_size+1));
				memcpy_s(result, (buff_size+1)*sizeof(struct event), buffer, sizeof(struct event)*(buff_size));
				buff_size++;
				free(buffer);
			}
	}
	return result;
}


mapPtr interprete_map_mapping(jPtr ptr){
	mapPtr result, buffer;
	int i, j, size, buff_len;
	result = 0;
	if(ptr->data_type == List && ptr->list_type == Object 
		&& strcmp("id", ptr->data[0].data[0].key) == 0 && strcmp("name", ptr->data[0].data[1].key) == 0){
			buff_len = 2;
			result = (mapPtr)malloc(sizeof(struct map)*buff_len);
			memset(result, 0, sizeof(struct map)*buff_len);
			for(i = 0; ptr->data[i].data_type != end; i++){
				for(j = 0; ptr->data[i].data[j].data_type != end; j++){
					size = strlen(ptr->data[i].data[j].string)+1;
					if(j == 0)
						result[i].map_id = atoi(ptr->data[i].data[j].string);
					else{
						result[i].map_name = (char*)malloc(size);
						strcpy_s(result[i].map_name, size, ptr->data[i].data[j].string);
					}
				}
				buffer = result;
				result = (mapPtr)malloc(sizeof(struct map)*(buff_len+1));
				memset(result, 0, sizeof(struct map)*(buff_len+1));
				memcpy_s(result, (buff_len+1)*sizeof(struct map), buffer, sizeof(struct map)*(buff_len));
				buff_len++;
				free(buffer);
			}
	}
	return result;
}


worldPtr interprete_world_mapping(jPtr ptr){
	worldPtr result, buffer;
	int i, j, size, buff_len;
	result = 0;
	if(ptr->data_type == List && ptr->list_type == Object 
		&& strcmp("id", ptr->data[0].data[0].key) == 0 && strcmp("name", ptr->data[0].data[1].key) == 0){
			buff_len = 2;
			result = (worldPtr)malloc(sizeof(struct world)*buff_len);
			memset(result, 0, sizeof(struct world)*buff_len);
			for(i = 0; ptr->data[i].data_type != end; i++){
				for(j = 0; ptr->data[i].data[j].data_type != end; j++){
					size = strlen(ptr->data[i].data[j].string)+1;
					if(j == 0)
						result[i].world_id = atoi(ptr->data[i].data[j].string);
					else{
						result[i].world_name = (char*)malloc(size);
						strcpy_s(result[i].world_name, size, ptr->data[i].data[j].string);
					}
				}
				buffer = result;
				result = (worldPtr)malloc(sizeof(struct world)*(buff_len+1));
				memset(result, 0, sizeof(struct world)*(buff_len+1));
				memcpy_s(result, (buff_len+1)*sizeof(struct world), buffer, sizeof(struct world)*(buff_len));
				buff_len++;
				free(buffer);
			}
	}
	return result;
}


eventPtr interprete_event_state(jPtr ptr){
	eventPtr result, buffer;
	int i, j, size, buff_len;

	result = 0;
	if(ptr->data_type == List && ptr->data->list_type == Object && strcmp(ptr->key, "events") == 0){
		buff_len = 2;
		result = (eventPtr)malloc(sizeof(struct event)*buff_len);
		memset(result, 0, sizeof(struct event)*buff_len);
		for(i = 0; ptr->data->data[i].data_type != end; i++){
			result[i].event_name = 0;
			for(j = 0; ptr->data->data[i].data[j].data_type != end; j++){
				if(j == 0)
					result[i].world_id = ptr->data->data[i].data[j].i;
				else if(j == 1)
					result[i].map_id = ptr->data->data[i].data[j].i;
				else if(j == 2){
					size = strlen(ptr->data->data[i].data[j].string)+1;
					result[i].event_id = (char*)malloc(size);
					strcpy_s(result[i].event_id, size, ptr->data->data[i].data[j].string);
				}
				else
					result[i].state = __interprete_state(ptr->data->data[i].data[j].string);
			}
			buffer = result;
			result = (eventPtr)malloc(sizeof(struct event)*(buff_len+1));
			memset(result, 0, sizeof(struct event)*(buff_len+1));
			memcpy_s(result, (buff_len+1)*sizeof(struct event), buffer, sizeof(struct event)*(buff_len));
			buff_len++;
			free(buffer);
		}
	}
	return result;
}


guildPtr interprete_guild_info(jPtr ptr){
	guildPtr result;
	int i;
	result = 0;
	if(ptr->data_type == String){
		i = 0;
		result = (guildPtr)malloc(sizeof(struct guild));
		i = strlen(ptr[0].string)+1;
		result->id = (char*)malloc(i);
		strcpy_s(result->id, i, ptr[0].string);
		i = strlen(ptr[1].string)+1;
		result->name = (char*)malloc(i);
		strcpy_s(result->name, i, ptr[1].string);
		i = strlen(ptr[2].string)+1;
		result->tag = (char*)malloc(i);
		strcpy_s(result->tag, i, ptr[2].string);
	}
	return result;
}